package com.lms.busi.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lms.busi.common.mapper.SysCommonCodeMapper;
import com.lms.busi.common.service.ISysCommonCodeService;
import com.lms.common.entity.SysCommonCode;
import com.lms.common.pure.entity.TreeNode;
import com.lms.common.pure.res.CodeRes;
import com.lms.common.web.service.impl.WrapperServiceImpl;
import com.lms.common.web.utils.CheckEntityResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SysCommonCodeServiceImpl extends WrapperServiceImpl<SysCommonCodeMapper, SysCommonCode> implements ISysCommonCodeService {
    @Override
    public IPage<?> queryMapper(Page<SysCommonCode> page, Map<String, Object> p) {
        return this.baseMapper.query(page, p);
    }

    @Override
    public SysCommonCode getByCode(String categoryCode, String code) {
        return this.baseMapper.getByCode(categoryCode, code);
    }

    @Override
    public List<CodeRes> getByCategory(String categoryCode) {
        return getByCategory(categoryCode, null);
    }

    @Override
    public List<CodeRes> getByCategory(String categoryCode, String parentId) {
        List<SysCommonCode> codes = this.getByCategoryWithEntity(categoryCode, parentId);
        List<CodeRes> result = new ArrayList<>();
        codes.forEach(code -> {
            result.add(new CodeRes(code.getName(), code.getCode(), code.getId(), code.getParentId()));
        });
        return result;
    }

    @Override
    public List<SysCommonCode> getByCategoryWithEntity(String categoryCode) {
        return getByCategoryWithEntity(categoryCode, null);
    }

    @Override
    public List<SysCommonCode> getByCategoryWithEntity(String categoryCode, String parentId) {
        QueryWrapper<SysCommonCode> params = new QueryWrapper<>();
        params.eq("category_code", categoryCode);
        if (parentId != null) {
            params.eq("parent_id", parentId);
        }
        params.orderByAsc("sort");
        return this.baseMapper.selectList(params);
    }

    @Override
    public void toggleStatus(String id) {
        SysCommonCode code = this.getById(id);
        boolean status = code.getStatus();
        code.setStatus(!status);
        this.updateById(code);
    }

    @Override
    public List<TreeNode> codeTree() {
        List<TreeNode> result = new ArrayList<>();
        // 先获取所有大类
        List<SysCommonCode> categoryList = this.baseMapper.selectCategory();
        categoryList.forEach(l -> {
            TreeNode node = new TreeNode();
            node.setName(l.getCategoryName());
            node.setId(l.getCategoryCode());
            result.add(node);
            // 根据大类查询码值
            List<SysCommonCode> res = this.getByCategoryWithEntity(l.getCategoryCode());
            res.forEach(i -> {
                TreeNode n = new TreeNode();
                n.setName(i.getName());
                n.setId(i.getId());
                node.addChild(n);
            });
        });
        return result;
    }

    @Override
    public CheckEntityResult beforeInsert(SysCommonCode entity) {
        entity.setStatus(true);
        // 判断sort排序
        if (entity.getSort() == null) {
            String categoryCode = entity.getCategoryCode();
            int sort = this.baseMapper.selectMaxSortByCategoryCode(categoryCode);
            entity.setSort(sort + 1);
        }
        return CheckEntityResult.getInstance();
    }
}
